package com.bq.gametrade.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.bq.gametrade.HttpLink;
import com.bq.gametrade.R;
import com.bq.gametrade.adapter.WithdrawalRecordsAdapter;
import com.bq.gametrade.base.BaseActivity;
import com.bq.gametrade.bean.UserInfoDetails;
import com.bq.gametrade.bean.WithdrawalRecords;
import com.bq.gametrade.bean.WithdrawalRecordsDetails;
import com.bq.gametrade.http.VRHttp;
import com.bq.gametrade.http.VRHttpListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class WithdrawalRecordsActivity extends BaseActivity implements View.OnClickListener{

    private ImageView back,ivService;
    private TextView tvTitle;

    private ArrayList<WithdrawalRecordsDetails> mList = new ArrayList<>();
    private WithdrawalRecordsAdapter adapter;
    private ListView listView;
    private Activity activity;
    private LinearLayout layPrompt;
    private UserInfoDetails userInfo;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_withdrawalrecords);
        if(application.isLogin()){
            userInfo = application.getUserInfo();
        }else {
            startActivity(new Intent(this, LoginActivity.class));
            finish();
        }
        activity = this;
        initView();
        getWithdrawalRecords();
    }


    private void initView() {
        back = serchViewById(R.id.image_back);
        back.setOnClickListener(this);
        showCustomerService(R.id.iv_userService);
        tvTitle = serchViewById(R.id.title_name);
        tvTitle.setText("提现记录");
        listView = serchViewById(R.id.lv_withdrawal);
        adapter = new WithdrawalRecordsAdapter(WithdrawalRecordsActivity.this,mList);
        listView.setAdapter(adapter);
        layPrompt = serchViewById(R.id.lay_prompt);

    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.image_back:
                finish();
                break;
        }
    }

    private void getWithdrawalRecords(){
        Map<String,String> params = new HashMap<>();
        params.put("token",userInfo.token);
        showSpotsDialog();
        VRHttp.getWithdrawalRecoredsList(activity, HttpLink.WITHDRAWAL_RECOREDS, params, new VRHttpListener() {
            @Override
            public void onSuccess(Object response, boolean isCache) {
                if(response != null){
                    WithdrawalRecords withdrawalRecords = (WithdrawalRecords)response;
                    if(withdrawalRecords.status.equals("0")){
                        mList = withdrawalRecords.cash_list;
                        if(mList.size()>0){
                            layPrompt.setVisibility(View.GONE);
                            adapter.setData(mList);
                            adapter.notifyDataSetChanged();
                        }else {
                            layPrompt.setVisibility(View.VISIBLE);
                        }
                        adapter.setData(mList);
                        adapter.notifyDataSetChanged();
                    }else if (withdrawalRecords.status.equals("99")) {
                        toastShow("登录超时");
                        application.setUserInfo(null);
                        startActivity(new Intent(activity, LoginActivity.class));
                        finish();
                    } else if (withdrawalRecords.status.equals("98")) {
                        toastShow("您的账号已在其它设备登录，请重新登录修改您的密码");
                        application.setUserInfo(null);
                        startActivity(new Intent(activity, LoginActivity.class));
                        finish();
                    }


                }

            }

            @Override
            public void onError(String error) {
                super.onError(error);
                toastShow("获取提现记录失败");
            }

            @Override
            public void onFinish() {
                super.onFinish();
                closeSpotsDialog();
            }
        });
    }
}
